define('views/select',
['jquery','views/datalist','bootstrapSelect'],
function($,datalist){

    function Selector() {
        this.zones = {}
    }

    Selector.prototype.Init = function () {
        var that = this
        this.InitSelect()

        $.ajax({
            url:'/src/assets/data/city-location.json',
            type:'get'
        }).done(function(resp){
            that.cityCenter=resp

        }).fail(function(error){console.error(error)})
    }

    // 构造行政区划下拉框并初始化数据，绑定下来框选择事件
    Selector.prototype.InitSelect = function () {
        var that = this
        this.StructSelect()

        $.ajax({
            url: '/src/assets/data/city.json',
            type: 'get'
        }).done(function(data){
            that.zones = data
            window.citys = that.zones
            var provices = []
            for (var provice in data) {
                provices.push(provice)
            }
            initProvice(provices)
            initCity()

        }).fail( function(error){console.error(error)} )

        // 添加省选项，只添加一次
        function initProvice(provice) {
            var options = ''
            provice.forEach(function(element) {
                options += '<option>'+element+'</option>'
            });
            $('#provice').html(options)
            that.StructSelect(12)
        }

        // 添加城市选项
        function initCity(){
            var defaultProvice = '北京'
            // 绑定省下拉框变好
            $('#provice').on('change',function(){
                initCityData($(this).val())
                that.DoSearch()
            })
            // 
            initCityData('北京')
            that.DoSearch()

            // 绑定城市下拉框变好
            $('#city').on('change',function(){
                that.DoSearch()
            })
            
            function initCityData(provice){
                var city = that.zones[provice]
                var citys = ''
                for(var key in city){
                    citys+='<option>'+key+'</option>'
                }
                $('#city').empty().html(citys)
                that.StructSelect(1)
            }
        }
    }


    /**
     * 格式化select组件
     */
    Selector.prototype.StructSelect = function (options) {
        if (!options) {
            $('#provice').selectpicker();
            $('#city').selectpicker();
        } else {
            if (options) {
                $('#provice').selectpicker('refresh');
                $('#city').selectpicker('refresh');
            }              
        }
    }

    // 根据行政区划获取选中市的数据
    Selector.prototype.DoSearch = function(){
        var url = 'http://39.99.193.71:8090/emergency/getDetailByCity?'
        var provice = $('#provice').val()
        var city = $('#city').val()
        // console.log('search', $('#provice').val(), $('#city').val())
        $.ajax({
            url:url+'province='+provice+'&city='+city,
            type:'get'
        }).done(function(response){
            console.log(response)
            if(window.areas){
                window.areas.removePoints()
                window.areas.addPoints(response)
            }
            if(datalist.ShowData){
                datalist.ShowData(response)
            }

        }).fail(function(error){console.error(error)})



        // 定位到選擇的城市
        if(this.cityCenter && this.cityCenter.length>0){
            for(var i=0;i<this.cityCenter.length;i++)
            {
                var center = this.cityCenter[i]
                if(city==center.name){
    
                    window.map.setView(L.latLng(center.lat,center.lng),10)
    
                    break
                }
            }
        }
        
        
    }

    return Selector
})